package database.mybatis.a_introduction;

import database.mybatis.entity.SysRole;
import database.mybatis.a_introduction.mapper.SysRoleMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        test1();
        System.out.println("===============");
        test2();
    }

    private static void test1() throws IOException {
        SqlSession session = getSession();
        SysRole sysRole = session.selectOne("database.mybatis.a_introduction.mapper.SysRoleMapper.getById","101");
        System.out.println(sysRole);
        List<SysRole> all= session.selectList("database.mybatis.a_introduction.mapper.SysRoleMapper.getById");
        System.out.println(all);// 返回空列表，不会报错
        List<SysRole> all1= session.selectList("database.mybatis.a_introduction.mapper.SysRoleMapper.getAll");
        System.out.println(all1);
    }

    private static void test2() throws IOException {
        SqlSession session = getSession();
        SysRoleMapper sysRoleMapper = session.getMapper(SysRoleMapper.class);
        SysRole sysRole = sysRoleMapper.getById("111");
        System.out.println("sysRole:" + sysRole);
        SysRole sysRole1 = sysRoleMapper.findById("101");
        System.out.println("sysRole1:" + sysRole1);
        List<SysRole> all = sysRoleMapper.getAll();
        System.out.println("all:" + all);
    }

    private static SqlSession getSession() throws IOException {
        String configFilePath = "database.mybatis/a_introduction/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(configFilePath);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory.openSession();
    }
}
